﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

namespace Forum.Web.Controllers
{
    public class ImageController : Controller
    {
        //
        // GET: /Image/

        private string DecToS26(int dec, string chars)
        {
            StringBuilder builder = new StringBuilder();
            while (dec > 0)
            {
                builder.Append(chars[dec % 26]);
                dec /= 26;
            }
            return builder.ToString();
        }

        public void ValidateCode()
        {
            const string chars = "ABCDEFGHAJKLMNAPQRSTUVWXYZ";


            Color[] m_Colors = new Color[] { Color.Black, Color.Red, Color.Green, Color.Blue, Color.Brown, Color.DarkMagenta, Color.DarkRed, Color.Indigo, Color.DarkBlue, Color.Maroon };
            Color BackgroundColor = Color.White;


            #region 颜色乱序
            //int n1 = DateTime.Now.Millisecond % 10;
            //int n2 = DateTime.Now.Second % 10;
            //if (n1 != n2)
            //{
            //    Color c1 = m_Colors[n1];
            //    m_Colors[n1] = m_Colors[n2];
            //    m_Colors[n2] = c1;
            //}
            #endregion

            #region 随机码生成
            Random r = new Random();
            int num = r.Next(1000, 100000);
            string code = num.ToString();
            string name = "ValidateCode" + Request.QueryString["p"] ?? string.Empty;
            string s26 = DecToS26(num / 1000, chars).Substring(0, 1);
            code = code.Substring(code.Length - 4, 4);
            code = code.Insert(DateTime.Now.Millisecond % 5, s26);

            Session.Add(name, code);
            //context.Session[name] = code;
            #endregion

            Color fcolor = m_Colors[DateTime.Now.Millisecond % 10];
            SolidBrush brush = new SolidBrush(fcolor);

            using (Bitmap image = new Bitmap(80, 22))
            {
                using (Graphics g = Graphics.FromImage(image))
                {
                    //背景
                    g.Clear(BackgroundColor);
                    //边框
                    g.DrawRectangle(new Pen(brush), 0, 0, 79, 21);

                    #region 随机干扰线生成
                    //int x1, x2, y1, y2;
                    //for (int i = 0; i < 5; i++)
                    //{
                    //    Pen p = new Pen(m_Colors[DateTime.Now.Second % 10], 1.5f);
                    //    x1 = r.Next(image.Width);
                    //    y1 = r.Next(image.Height);
                    //    x2 = r.Next(image.Width);
                    //    y2 = r.Next(image.Height);
                    //    g.DrawLine(p, x1, y1, x2, y2);
                    //}
                    #endregion

                    #region 随机噪点生成
                    for (int i = 0; i < image.Width * 2; i++)
                    {
                        int x = r.Next(image.Width);
                        int y = r.Next(image.Height);
                        image.SetPixel(x, y, fcolor);
                    }
                    #endregion

                    for (int i = 0; i < 5; i++)
                    {
                        string c = code.Substring(i, 1);
                        int v = (int)c[0];
                        Font f = (v + DateTime.Now.Millisecond) % 2 == 0 ? new Font("Verdana", 14 - (DateTime.Now.Second % 2) / 2) : new Font("Verdana", 14 + (DateTime.Now.Second % 2) / 2, FontStyle.Italic);
                        g.DrawString(c, f, brush, 4 + i * 14 - DateTime.Now.Millisecond % 2, DateTime.Now.Second % 2);
                    }

                    using (MemoryStream ms = new MemoryStream())
                    {
                        image.Save(ms, ImageFormat.Gif);
                        Response.ClearContent();
                        Response.ContentType = "image/Gif";
                        Response.Expires = -1;
                        Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
                        Response.Cache.SetNoStore();
                        Response.BinaryWrite(ms.ToArray());
                        Response.End();
                    }
                }

            }

        }

        public ContentResult CheckCode(string key, string value)
        {
            string name = "ValidateCode" + key;
            string flag = "false";
            if (Session[name] != null)
            {
                if (Session[name].ToString().ToUpper() == value.ToUpper())
                {
                    flag = "true";
                }
            }

            return Content(flag);

        }

    }
}
